import AuthApi from '@/api/auth.js';
import {
  cearedRoute
} from './cearedRoute.js'
import {
  createdAuth
} from '@/utils/createdAuth'
import {
  errpage
} from '@/router/baseRouter.js'
const user = {

  namespaced: true,

  /*状态值*/
  state: {
	menus:null,
    roles: null,
    page: null
  },

  /*状态值转换*/
  getters: {

    getroles: (state) => (name) => {
      return state.roles;
    },
    getmenus: (state) => (name) => {
      return state.menus;
    },
  },

  /*改变状态的方法 不可异步*/
  mutations: {

    setState(state, value) {
      state[value.key] = value.val;
    },

    // saveMenulist(state, value) {
    //   state.getters.menulist = value;
    // }

  },

  /*可异步改变*/
  actions: {

    generateRoutes: function(context, str) {
      /*返回*/
      return new Promise((resolve, reject) => {
		  
			// 获取菜单
			AuthApi.getAccessList(context).then((res) => {
				// res.data.data是全部权限
				let data = res.data.data
				const menu = [];
				let routerlist = null;
				// 所有菜单
				let list = cearedRoute(data);
				// routerlist是左侧菜单或者独立页
				routerlist = menu.concat(list, errpage);
				// 存path,验证按钮
				  let authlist = {}
				  createdAuth(data, authlist);
				  // console.log(authlist)
				  sessionStorage.setItem("authlist",JSON.stringify(authlist));
				// 存储所有权限
				context.commit("setState", {
				  key: 'menus',
				  val: data
				});
				resolve(routerlist);
			}).catch(error => {
              reject(error);
            });
			
      })
    }

  }
}


export default user;
